<?php
namespace app\servercertificate\logic;
use \app\manage\logic\BaseLogic;
use \think\Db;
/**
 * @author:      李颖
 * @DateTime:    2017-10-23 16:07:49
 * @Description: 证书 - 提货
 */
class Pickupgoods extends BaseLogic
{
	protected $name = 'pick_up_goods';
    protected $config = 'db_config_certificate';
    protected $pickupgoodsModel = 'app\servercertificate\model\Pickupgoods';
    protected $certificateModel = 'app\servercertificate\model\Certificate';


    /*我的证书 - 提货*/
    // 该证书变为不可售状态
    public function pickUpGoods($data): array 
    {
        // 修改证书为不可售状态
        $_whereCerti['id'] = $data['certificate_id'];
        $_whereCerti['user_id'] = $data['user_id'];
        $updateCerti = $this->model($this->certificateModel)->save(['is_sell'=>0], $_whereCerti);
        if(!$updateCerti){ return verify_err(null, '修改证书不可售状态失败'); }

    	$_where['certificate_id'] = $data['certificate_id'];
    	$_where['user_id'] = $data['user_id'];
    	$info = $this->model($this->pickupgoodsModel)->where($_where)->find();
    	if($info){
    		return verify_err(null, '您已填写了该证书的提货单');
    	}
    	$res = $this->model($this->pickupgoodsModel)->save($data);
    	return auto_return($res);
    }

    /*获取我的某证书的提货详情*/
    public function onePickUpInfo($data): array 
    {
    	isset($data['user_id']) && $_where['pick_up_goods.user_id'] = $data['user_id'];
    	$_where['pick_up_goods.certificate_id'] = $data['certificate_id'];
    	$_where['pick_up_goods.status'] = 1;
    	$res = Db::connect(config('db_config_certificate'))
    			->view('pick_up_goods', '*')
    			->view('certificate', 'certificate_price,pay_method,order_info as certificate_order_info,name as certificate_name,product_code as certificate_product_code,earnings as certificate_earnings,is_pick as certificate_is_pick', 'pick_up_goods.certificate_id = certificate.id', 'LEFT')
    			->where($_where)
    			->order('pick_up_goods.id desc')
    			->find();
        if($res){
            $res['certificate_order_info'] = json_decode($res['certificate_order_info'], true);
        }
    	return auto_return($res);
    }
    /*获取我的简单的提货详情*/
    public function simplePickUpInfo($data): array 
    {
        $_where['certificate_id'] = $data['certificate_id'];
        $_where['user_id'] = $data['user_id'];
        $res = $this->model($this->pickupgoodsModel)->where($_where)->find();
        return auto_return($res);
    }

    /*后台管理员对某证书进行发货操作*/
    public function shipmentsGoods($data): array 
    {
        $_where['certificate_id'] = $data['certificate_id'];
        $_where['status'] = 1;
        $_where['transfer_status'] = 0;
        $saveData['transfer_status'] = 1;
        $saveData['shipments_time'] = NOW;
        $saveData['logistics_company_code'] = $data['logistics_company_code'];
        $saveData['logistics_company_name'] = $data['logistics_company_name'];
        $saveData['waybill_number'] = $data['waybill_number'];
        $res = $this->model($this->pickupgoodsModel)->save($saveData, $_where);
        return auto_return($res, '成功对该证书进行发货', '对该证书进行发货失败');
    }

    /*获取我的提货单列表*/
    public function myPickUpList($data): array 
    {
        $_where['p.user_id'] = $data['user_id'];
        $_where['p.status'] = 1;
        $list = $this->model($this->pickupgoodsModel)
                    ->alias('p')
                    ->field('p.*,i.status as invoice_status')
                    ->join('invoice i', 'i.pick_up_id = p.id', 'LEFT')
                    ->where($_where)
                    ->order('id desc')
                    ->paginate(null, false, ['page'=>$data['page'], 'list_rows'=>$data['listRows']])
                    ->toArray();
        return auto_return_page($list, '成功获取提货单列表', '获取提货单列表失败');
    }

    /*获取我的申请提货数量*/
    public function myApplyPickNumber($data): array 
    {
        $_where['user_id'] = $data['user_id'];
        $_where['status'] = 1;
        $_where['pick_up_time'] = 0;
        $count = $this->model($this->pickupgoodsModel)->where($_where)->count();
        return auto_return($count, '成功获取申请提货数量', '获取申请提货数量失败');
    }

    /*后台管理员获取提货单列表*/
    public function pickUpGoodsList($data): array 
    {
        $data['keywords'] && $_where['c.name|pick_up_code|reason|waybill_number'] = ['like', '%' . $data['keywords'] . '%'];
        $data['transfer_status'] !== '' && $_where['transfer_status'] = $data['transfer_status'];
        $_where['p.status'] = 1;
        $list = $this->model($this->pickupgoodsModel)
                    ->alias('p')
                    ->field('p.*, c.name as certificate_name,c.earnings,c.is_pick,c.product_code as certificate_product_code')
                    ->join('certificate c', 'c.id = p.certificate_id', 'LEFT')
                    ->where($_where)
                    ->order('id desc')
                    ->paginate(null, false, ['page'=>$data['page'], 'list_rows'=>$data['listRows']])
                    ->toArray();
        return auto_return_page($list, '成功获取提货单列表', '获取提货单列表失败');
    }


}